//
//  PCDApiLogin.m
//  PCDMPaasBank
//
//  Created by DK on 2021/3/8.
//  Copyright © 2021 Alibaba. All rights reserved.
//

#import "PCDApiLogin.h"
#import "CTMediator+CTMediatorModuleActions.h"
#import "PCDDataBaseModel.h"
@implementation PCDApiLogin

- (void)handler:(id)data andContext:(PCDContext *)context ResponseCallback:(WVJBResponseCallback )responseCallback
{
      
    PCDBankWebViewController *bankWebViewController = context.viewController;
    NSString *menuUrl = data[@"menuUrl"];
    NSString *menuNo = data[@"menuNo"];
    NSString *LoginType = data[@"LoginType"];
    NSDictionary *params;
    if ([LoginType isEqualToString:@"1"]) {
        params = @{@"ShowType":[NSNumber numberWithInt:0],@"FromWeb":@"Y", @"LoginType":@"1"};
        [PCDUtil saveLoginType:0];
    }else {
        params = @{@"ShowType":[NSNumber numberWithInt:0],@"FromWeb":@"Y"};
    }
    //退出登录
    [PCDUtil saveLoginSession:nil];
    //清空内存缓存信息
    [[PCDDataBaseModel shareModel].memoryStorage removeAllObjects];
    
    //回到首页
    [bankWebViewController.tabBarViewController setSelectedIndex:0];
    
    UIViewController *viewController= [[CTMediator sharedInstance] CTMediatorLoginViewController:params CompleteAction:^(BOOL error, id session) {
        if (error) {
            WKWebView *webView = context.webView;
            [webView reload];
        }else {
            if (![PCDUtil nilOrEmptyString:menuNo]) {
                [PCDRouteService openBankWebUrl:menuUrl withPackageID:menuNo isNeedLogin:YES parent:PCDUtil.getCurrentVC];
            }
        }
    }];
    
    [bankWebViewController.navigationController pushViewController:viewController animated:YES];

    //清理所有栈内页面
    NSMutableArray *viewCtrs = [self clearViews];
    [PCDPageViewMangeGet().baseNavgatView setViewControllers:viewCtrs animated:YES];
}

-(NSMutableArray *)clearViews
{
    NSMutableArray *viewCtrs = [NSMutableArray arrayWithArray:PCDPageViewMangeGet().baseNavgatView.viewControllers];
    
    
    NSMutableArray *newArray = [NSMutableArray new];
    for (id view in viewCtrs) {
        if (![view isKindOfClass:[PCDBankWebViewController class]]) {
            [newArray addObject:view];
        }
    }
    return newArray;
}



@end
